package com.py.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.py.payment.dto.param.MonitorParamDto;
import com.py.payment.dto.ret.MonitorRetDto;
import com.py.payment.entity.*;
import com.py.payment.mapper.MonitorMapper;
import com.py.payment.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.py.payment.tools.ToolsUtils;
import com.py.payment.tools.date.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 监测记录 服务实现类
 * </p>
 *
 * @author py
 * @since 2024-05-21
 */
@Service
public class MonitorServiceImpl extends ServiceImpl<MonitorMapper, Monitor> implements MonitorService {

    @Autowired
    private EarlyLevelConfigService earlyLevelConfigService;

    @Autowired
    private PlanRecordsService planRecordsService;

    @Autowired
    private PlanInfoService planInfoService;

    @Autowired
    private EarlyInfoService earlyInfoService;

    @Override
    public List<MonitorRetDto> getPredictionList(MonitorParamDto monitorParamDto) {
        List<MonitorRetDto> monitorRetDtoList = this.baseMapper.getPredictionList(monitorParamDto);
        monitorRetDtoList.forEach(item->{
            if(ToolsUtils.isNotEmpty(item.getMinValue()) && item.getMinValue().compareTo(BigDecimal.ZERO) != 0){
                if(item.getActual().compareTo(item.getMinValue()) < 0){
                    item.setIsRed(2);
                }
            }
            if(ToolsUtils.isNotEmpty(item.getMaxValue()) && item.getMaxValue().compareTo(BigDecimal.ZERO) != 0){
                if(item.getActual().compareTo(item.getMaxValue()) > 0){
                    item.setIsRed(2);
                }
            }
            item.setCreateTimeStr(DateUtils.dateToDateStr(item.getCreateTime(),DateUtils.YYYY_MM_DD_HH_MM_SS));
        });
        return monitorRetDtoList;
    }

    @Override
    public List<Monitor> getStatus() {
        return this.baseMapper.getStatus();
    }

    @Override
    @Transient
    public void addMonitor(List<Monitor> monitorList) {
        List<EarlyInfo> earlyInfos = this.earlyLevelConfigService.earlyRating(monitorList);
        List<PlanRecords> planRecords = new ArrayList<>();
        earlyInfos.forEach(item->{
            QueryWrapper<PlanInfo> planInfoQueryWrapper = new QueryWrapper<>();
            planInfoQueryWrapper.eq("early_level_id", item.getEarlyLevel());
            List<PlanInfo> planInfoList = this.planInfoService.list(planInfoQueryWrapper);
            planInfoList.forEach(p->{
                PlanRecords pr = new PlanRecords();
                pr.setPlanId(p.getId());
                pr.setReason("实际值超出预警值");
                pr.setCreateTime(item.getCreateTime());
                planRecords.add(pr);
            });
        });
        this.earlyInfoService.saveBatch(earlyInfos);
        this.planRecordsService.saveBatch(planRecords);
        this.saveBatch(monitorList);
    }
}

